package cn.stylefeng.guns.core.schedue.quartz;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.recy.entity.Bins;
import cn.stylefeng.guns.modular.recy.entity.CommandOrder;
import cn.stylefeng.guns.modular.recy.service.BinsService;
import cn.stylefeng.guns.modular.recy.service.CommandOrderService;
import cn.stylefeng.guns.modular.recy.utils.QuartzJob;
import cn.stylefeng.guns.modular.recy.utils.QuartzManager;
import org.apache.commons.lang3.math.NumberUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * quartz的启动示例
 *
 * @author fengshuonan
 * @Date 2019/2/24 16:55
 */
@Component
public class StartQuartzExample implements CommandLineRunner {

    @Autowired
    private BinsService binsService;

    @Override
    public void run(String... args) throws Exception {
        List<Bins> binsList
                = this.binsService.list();
        for(Bins bins:binsList){
            String statusDesc = bins.getStatusDesc();
            if(StrUtil.isNotBlank(statusDesc)) {
                String[] statusDescSplit = statusDesc.split(",");
                String[] split = statusDescSplit[0].split("-");
                String xsBeginTime = split[0];
                String xsEndTime = split[1];
                String xsTime = statusDescSplit[1];
                String xsTimeGap = statusDescSplit[2];
                JobDataMap openJobDataMap = new JobDataMap();
                openJobDataMap.put("binsId", bins.getId());
                openJobDataMap.put("commandTag", "sterilize_open");
                int xsBeginTimeInt = NumberUtils.toInt(xsBeginTime);
                int xsEndTimeInt = NumberUtils.toInt(xsEndTime);
                int xsTimeGapInt = NumberUtils.toInt(xsTimeGap);
                int xsTimeHourGapInt = xsTimeGapInt/60;
                int xsTimeMinuteInt = xsTimeGapInt%60;
                String hour = "";
                for(int i=xsBeginTimeInt;i<xsEndTimeInt+1;i=i+xsTimeHourGapInt){
                    hour+=i+",";
                }
                hour = StrUtil.removeSuffix(hour,",");

                String minute = "";
                if(xsTimeMinuteInt>0){
                    minute = "0/"+xsTimeMinuteInt;
                }else{
                    minute = xsTimeMinuteInt+"";
                }

                String timeOpen = "0 "+minute+" "+hour+" * * ?";
                QuartzManager.addJob(bins.getId()+"",QuartzJob.class,timeOpen, openJobDataMap);

                JobDataMap closeJobDataMap = new JobDataMap();
                closeJobDataMap.put("binsId",bins.getId());
                closeJobDataMap.put("commandTag","sterilize_close");
                String timeClose = "0 "+xsTime+"/"+xsTimeMinuteInt+" "+hour+" * * ?";
                QuartzManager.addJob(bins.getId()+"_close",QuartzJob.class,timeClose, closeJobDataMap);
            }
        }

    }
}